package com.dpp.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.dpp.config.WxInformation;
import com.dpp.entity.AddressBook;
import com.dpp.entity.Users;
import com.dpp.mapper.AddressMapper;
import com.dpp.mapper.UserMapper;
import com.dpp.service.UserService;
import com.dpp.util.GoogleBarCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Value("${dpp.stu-path}")
    private String basePath;
    @Autowired
     private WxInformation wxInformation;
    @Autowired
    private ActivityRecordServiceImpl activityRecordService;
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 登录
     *
     * @param code
     * @return
     */
    @Override
    @Transactional
    public Users login(String code) {
        //code获取openid  定义请求地址,替换参数
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
                +wxInformation.getPublicAppid()+"&secret="+wxInformation.getPublicAppsecret()+
                "&code="+code+"&grant_type=authorization_code";
        //发送请求 的到返回结果
        String token = HttpUtil.get(url);
        //先把String对象转换成json对象
        JSONObject tokenobj= JSONObject.parseObject(token);
        String openId= (String) tokenobj.get("openid");;
        log.info("用户登录注册:{}",openId);
        if(openId==null){
            return null;
        }
        Users user = userMapper.selectByOpenId(openId);
        //第一次登录
        if(user==null){
            //添加到用户表
            Users users = new Users();
            users.setOpenid(openId);
            users.setType(0);
            users.setOrderTimes(0);
            userMapper.save(users);
//           activityRecordService.CouponInsert(4,openId);
            return users;
        }
        //数据库已有数据
        return user;

    }

    @Override
    public String getOpenid(String code) {
        //code获取openid  定义请求地址,替换参数
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
                +wxInformation.getPublicAppid()+"&secret="+wxInformation.getPublicAppsecret()+
                "&code="+code+"&grant_type=authorization_code";
        //发送请求 的到返回结果
        String token = HttpUtil.get(url);
        //先把String对象转换成json对象
        JSONObject tokenobj= JSONObject.parseObject(token);
        String openId= (String) tokenobj.get("openid");;
        return openId;
    }

    @Override
    public BufferedImage showQR(String openId) throws IOException {
        AddressBook addressBook = addressMapper.selectByOpenid(openId);
        if(addressBook==null){
            return null;
        }
        return GoogleBarCodeUtils.insertWords(GoogleBarCodeUtils.getBarCode(addressBook.getPhone()));
    }

}




